package com.ltu.m7011e.imagine.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.ltu.m7011e.imagine.arithmetics.client.Arithmetics;
import com.ltu.m7011e.imagine.client.ui.MenuLink;
import com.ltu.m7011e.imagine.client.ui.MenuPanel;
import com.ltu.m7011e.imagine.login.client.Login;
import com.ltu.m7011e.imagine.sudoku.client.Sudoku;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Imagine implements EntryPoint, ValueChangeHandler<String>
{
	private AsyncCallback<String> callback;
	private PagesServiceAsync reverserService;
	
	public static final String INIT_STATE = "Latest";
	
	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad()
	{
		setUpRPC();
		
		menu();
		
		initHistoryState();
	}
	
	public void initHistoryState()
	{
		String token = History.getToken();
		
		if(token.length() == 0)
		{
			onHistoryChange(INIT_STATE);
		}
		else
		{
			onHistoryChange(token);
		}
	}
	
	public void onHistoryChange(String token)
	{
		if(token.compareTo("Games/Suduku") == 0)
		{
			pageBrowse();
		}
		else if(token.compareTo("Sudoku") == 0)
		{
			pageBrowse();
		}
		else if(token.compareTo("Addition") == 0)
		{
			pageScore();
		}
		else if(token.compareTo("Login") == 0)
		{
			pageLogin();
		}
		else
		{
			reverserService.getPage(token, callback);
		}
	}
	
	public void onValueChange(ValueChangeEvent<String> event)
	{
		onHistoryChange(event.getValue());
	}
	
	private void pageBrowse()
	{
		new Sudoku();
	}
	
	private void pageScore()
	{
		new Arithmetics();
	}
	
	private void pageLogin()
	{
		new Login();
	}
	
	private void menu()
	{
		MenuPanel menu = new MenuPanel();
		
		MenuLink latest = new MenuLink("Latest");
		//MenuLink browse = new MenuLink("Browse");
		//MenuLink score = new MenuLink("Score");
		MenuLink browse = new MenuLink("Sudoku");
		MenuLink score = new MenuLink("Addition");
		MenuLink register = new MenuLink("Register");
		MenuLink login = new MenuLink("Login");
		
		latest.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				reverserService.getPage("Latest", callback);
			}
		});
		
		browse.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				pageBrowse();
				//reverserService.getPage("Browse", callback);
			}
		});
		
		score.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				pageScore();
				//reverserService.getPage("Score", callback);
			}
		});
		
		register.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				reverserService.getPage("Register", callback);
			}
		});
		
		login.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				pageLogin();
				//reverserService.getPage("Login", callback);
			}
		});
		
		menu.add(latest);
		menu.add(browse);
		menu.add(score);
		menu.add(register);
		menu.add(login);
		
		RootPanel.get("menu").add(menu);
	}
	
	private void setUpRPC()
	{
		reverserService = (PagesServiceAsync) GWT.create(PagesService.class);
		((ServiceDefTarget) reverserService).setServiceEntryPoint(GWT.getModuleBaseURL() + "Pages/PagesService");
		callback = new AsyncCallback<String>()
		{
			// This method will be called if the service call fails   
			public void onFailure(Throwable caught)
			{
				// Show a message informing the user why the call failed   
				Window.alert("Failed to get response from server" + caught.getMessage());
			}
			
			// This method will be called if the service call succeeds   
			public void onSuccess(String result)
			{
				// Get the service call result and cast it to the desired type and display it
				HTML widget = new HTML(result);
				
				RootPanel.get("content").clear();
				RootPanel.get("content").add(widget);
			}
		};
	}
}
